<template>
  <!-- 表单弹窗 -->
  <el-dialog
    :close-on-click-modal="false"
    v-model="visibleDialog"
    :title="props.title"
    width="50%"
    draggable
    @close="closeDialog"
  >
    <el-form ref="formRef" :model="formData" :rules="rules" label-width="150px">
      <el-row>
        <el-col :span="12">
          <el-form-item label="服务编码" prop="serviceCode">
            <el-input
              v-model="formData.serviceCode"
              placeholder="请输入服务编码"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="服务名称" prop="serviceName">
            <el-input
              v-model="formData.serviceName"
              placeholder="请输入服务名称"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="交互方式" prop="exchangeMode">
            <el-select
              v-model="formData.exchangeMode"
              placeholder="请选择交互方式"
              clearable
            >
              <el-option
                v-for="item in exchangeModeList"
                :key="item.value"
                :value="item.value"
                :label="item.label"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="接口" prop="interfaceCode">
            <el-select
              v-model="formData.interfaceCode"
              placeholder="请选择接口"
              style="width: 100%"
            >
              <el-option
                v-for="item in interfaceList"
                :key="item.interfaceCode"
                :value="item.interfaceCode"
                :label="item.interfaceName"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="鉴权方式" prop="authMode">
            <el-select
              v-model="formData.authMode"
              placeholder="请选择鉴权方式"
              clearable
            >
              <el-option
                v-for="item in authModeList"
                :key="item.value"
                :value="item.value"
                :label="item.label"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="鉴权信息" prop="authInfo">
        <el-input
          type="textarea"
          v-model="formData.authInfo"
          placeholder="请输入鉴权信息"
        />
      </el-form-item>
      <el-row>
        <el-col :span="12">
          <el-form-item label="故障重试(次)" prop="failureRetryCount">
            <el-input-number
              v-model="formData.failureRetryCount"
              style="width: 100px"
              controls-position="right"
              :min="0"
              :max="10"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="日志保留(天)" prop="logRetainTime">
            <el-input-number
              v-model="formData.logRetainTime"
              style="width: 100px"
              controls-position="right"
              :min="0"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="调用说明" prop="callExplain">
        <el-input v-model="formData.callExplain" placeholder="请输入调用说明" />
      </el-form-item>
      <el-row>
        <el-col :span="12">
          <el-form-item label="是否启用测试">
            <el-radio-group v-model="formData.testFlag">
              <el-radio :label="0">否</el-radio>
              <el-radio :label="1">是</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="启用标志" prop="enabled">
            <el-radio-group v-model="formData.enabled">
              <el-radio :label="1">正常</el-radio>
              <el-radio :label="0">停用</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="备注" prop="remarks">
        <el-input v-model="formData.remarks" placeholder="请输入备注" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" :loading="loading" @click="handleSubmit"
          >确 定</el-button
        >
        <el-button @click="closeDialog">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import request from "@/utils/request";

defineOptions({
  name: "DEEXCHANGEINFOADDORUPDATE",
  inheritAttrs: false,
});

const emit = defineEmits(["reset-table", "change-visible"]);

const props = defineProps({
  visible: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
  title: {
    type: String,
    default: () => {
      return "";
    },
  },
  data: {
    type: Object,
    default: () => {
      return {};
    },
  },
});

const visibleDialog = ref(false);
watch(
  () => props.visible,
  (newVal: boolean) => {
    visibleDialog.value = newVal;
  }
);

const rules = reactive({
  serviceCode: [
    { required: true, message: "请输入服务编码", trigger: "blur" },
    { min: 1, max: 60, message: "长度在 1 到 60 个字符", trigger: "blur" },
  ],
  serviceName: [
    { required: true, message: "请输入服务名称", trigger: "blur" },
    { min: 1, max: 100, message: "长度在 1 到 100 个字符", trigger: "blur" },
  ],
  exchangeMode: [
    { required: true, message: "请输入交互方式", trigger: "blur" },
  ],
  authMode: [{ required: true, message: "请输入鉴权方式", trigger: "blur" }],
  authInfo: [
    { min: 1, max: 255, message: "长度在 1 到 255 个字符", trigger: "blur" },
  ],
  interfaceCode: [
    { required: true, message: "请输入接口编码", trigger: "blur" },
    { min: 1, max: 60, message: "长度在 1 到 60 个字符", trigger: "blur" },
  ],
  failureRetryCount: [
    { required: true, message: "请输入故障重试次数", trigger: "blur" },
  ],
  logRetainTime: [
    { required: true, message: "请输入日志保留时间(天)", trigger: "blur" },
  ],
  callExplain: [
    { min: 1, max: 100, message: "长度在 1 到 100 个字符", trigger: "blur" },
  ],
  testFlag: [{}],
  remarks: [
    { min: 1, max: 255, message: "长度在 1 到 255 个字符", trigger: "blur" },
  ],
  enabled: [],
});

const formRef = ref(ElForm);

let formData = reactive({
  id: "",
  version: "",
  serviceCode: "",
  serviceName: "",
  exchangeMode: "",
  authMode: "",
  authInfo: "",
  interfaceCode: "",
  failureRetryCount: 2,
  logRetainTime: 365,
  callExplain: "",
  testFlag: 0,
  remarks: "",
  enabled: 1,
});

watch(
  () => props.data,
  (newVal: any) => {
    if (newVal?.id) {
      const {
        id,
        version,
        serviceCode,
        serviceName,
        exchangeMode,
        authMode,
        authInfo,
        interfaceCode,
        failureRetryCount,
        logRetainTime,
        callExplain,
        testFlag,
        remarks,
        enabled,
        deleted,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
      } = newVal;
      Object.assign(formData, {
        id,
        version,
        serviceCode,
        serviceName,
        exchangeMode,
        authMode,
        authInfo,
        interfaceCode,
        failureRetryCount,
        logRetainTime,
        callExplain,
        testFlag,
        remarks,
        enabled,
        deleted,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
      });
    }
  }
);

/** 关闭表单弹窗 */
function closeDialog() {
  visibleDialog.value = false;
  emit("change-visible", false);
  resetForm();
}

/** 重置表单 */
function resetForm() {
  formRef.value.resetFields();
  formRef.value.clearValidate();
  formData.id = "";
  formData.version = "";
  formData.serviceCode = "";
  formData.serviceName = "";
  formData.exchangeMode = "";
  formData.authMode = "";
  formData.authInfo = "";
  formData.interfaceCode = "";
  formData.failureRetryCount = 2;
  formData.logRetainTime = 365;
  formData.callExplain = "";
  formData.testFlag = 0;
  formData.remarks = "";
  formData.enabled = 1;
}

const loading = ref(false);

/** 数据交互服务保存提交 */
function handleSubmit() {
  formRef.value.validate(async (valid: any) => {
    if (valid) {
      loading.value = true;
      const text = formData.id ? "编辑" : "新增";
      await request({
        url: "/dataExchange/exchangeInfo/save",
        method: "post",
        data: formData,
      }).finally(() => {
        loading.value = false;
      });
      ElMessage.success(text + `成功`);
      emit("reset-table", formData.id);
      closeDialog();
    }
  });
}

//获取接口列表
const interfaceList: any = ref([]);
async function getInterfaceList() {
  const { data } = await request({
    url: `/dataExchange/exchangeInterface/getList`,
    method: "post",
    data: {},
  });
  interfaceList.value = data;
}
//获取交互方式列表
const exchangeModeList: any = ref([]);
async function getExchangeModeList() {
  const { data } = await request({
    url: `/system/dictionary/getSelectedItems/ExchangeMode`,
    method: "post",
  });
  exchangeModeList.value = data;
}
//鉴权方式
const authModeList: any = ref([]);
async function getAuthModeList() {
  const { data } = await request({
    url: `/system/dictionary/getSelectedItems/DeAuthMode`,
    method: "post",
  });
  authModeList.value = data;
}
onMounted(() => {
  getAuthModeList();
  getExchangeModeList();
  getInterfaceList();
});

// 组件销毁时调用
onBeforeUnmount(() => {
  visibleDialog.value = false;
  emit("change-visible", false);
});
</script>
